<template>
  <el-dialog
    :close-on-click-modal="false"
    :visible.sync="dialogFormVisible"
    :before-close="close"
    width="800px"
    :top="'10vh'"
  >
    <el-form ref="form" :model="form" size="small" label-width="120px">
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item
            label="主题名称"
            prop="name"
            :rules="[{ required: true, message: '请输入主题名称', trigger: 'blur' }]"
            style="width: 100%"
          >
            <el-input v-model="form.name" placeholder="请输入主题名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="标准字体大小"
            prop="fontSize"
            :rules="[{ required: true, message: '标准字体大小', trigger: 'blur' }]"
            style="width: 100%"
          >
            <el-input-number v-model="form.fontSize" :min="1" label="标准字体大小" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="标准字体颜色"
            prop="fontColor"
            :rules="[{ required: true, message: '标准字体颜色', trigger: 'blur' }]"
            style="width: 100%"
          >
            <el-color-picker v-model="form.fontColor" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="背景色"
            prop="backGroundColor"
            :rules="[{ required: true, message: '背景色', trigger: 'blur' }]"
            style="width: 100%"
          >
            <el-color-picker v-model="form.backGroundColor" show-alpha />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="是否启用背景图"
            prop="sfqybjt"
            :rules="[{ required: true, message: '是否启用背景图', trigger: 'change' }]"
          >
            <el-radio-group v-model="form.sfqybjt">
              <el-radio
                v-for="item in status"
                :key="item.id"
                :label="item.value"
              >{{ item.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col v-if="form.sfqybjt === true" :span="24">
          <el-form-item
            label="背景图"
            prop="bakImage"
            :rules="[{ required: true, message: '请上传背景图', trigger: 'change' }]"
            style="width: 100%"
          >
            <custom-image-upload v-model="form.bakImage">
              <div slot="reference">
                <img v-if="form.bakImage" :src="form.bakImage" class="avatar" style="height: 60px">
                <i v-else class="el-icon-plus avatar-uploader-icon" />
              </div>
            </custom-image-upload>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            label="学校Logo"
            prop="logo"
            :rules="[{ required: true, message: '请上传学校Logo', trigger: 'change' }]"
            style="width: 100%"
          >
            <custom-image-upload v-model="form.logo">
              <div slot="reference">
                <img v-if="form.logo" :src="form.logo" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon" />
              </div>
            </custom-image-upload>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="图片灰度"
            prop="tphd"
            :rules="[{ required: true, message: '图片灰度', trigger: 'change' }]"
          >
            <el-radio-group v-model="form.tphd">
              <el-radio
                v-for="item in status"
                :key="item.id + '-d'"
                :label="item.value"
              >{{ item.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="栏目透明度"
            prop="opacity"
            :rules="[{ required: true, message: '标准字体大小', trigger: 'change' }]"
            style="width: 100%"
          >
            <el-input-number v-model="form.opacity" :precision="1" :step="0.1" :max="1" :min="0" label="标准字体大小" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="栏目背景色"
            prop="columnGroundColor"
            :rules="[{ required: true, message: '栏目背景色', trigger: 'blur' }]"
            style="width: 100%"
          >
            <el-color-picker v-model="form.columnGroundColor" show-alpha />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="标题背景色"
            prop="columnTitleGroundColor"
            :rules="[{ required: true, message: '标题背景色', trigger: 'blur' }]"
            style="width: 100%"
          >
            <el-color-picker v-model="form.columnTitleGroundColor" show-alpha />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="标题字体颜色"
            prop="columnTitleFontColor"
            :rules="[{ required: true, message: '标题字体颜色', trigger: 'blur' }]"
            style="width: 100%"
          >
            <el-color-picker v-model="form.columnTitleFontColor" show-alpha />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="选中色"
            prop="changeBackGroundColor"
            :rules="[{ required: true, message: '选中色', trigger: 'blur' }]"
            style="width: 100%"
          >
            <el-color-picker v-model="form.changeBackGroundColor" show-alpha />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="选中字体色"
            prop="changeFontColor"
            :rules="[{ required: true, message: '选中字体色', trigger: 'blur' }]"
            style="width: 100%"
          >
            <el-color-picker v-model="form.changeFontColor" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="边框大小"
            prop="bk"
            :rules="[{ required: true, message: '边框大小', trigger: 'blur' }]"
            style="width: 100%"
          >
            <el-input-number v-model="form.bk" :min="1" label="标准字体大小" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="边框颜色"
            prop="bkys"
            :rules="[{ required: true, message: '边框颜色', trigger: 'blur' }]"
            style="width: 100%"
          >
            <el-color-picker v-model="form.bkys" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="圆角大小"
            prop="bkdx"
            :rules="[{ required: true, message: '请输入圆角大小', trigger: 'blur' }]"
            style="width: 100%"
          >
            <el-input v-model="form.bkdx" placeholder="请输入圆角大小" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="状态"
            prop="zt"
            :rules="[{ required: true, message: '状态', trigger: 'change' }]"
          >
            <el-radio-group v-model="form.zt">
              <el-radio
                v-for="item in status"
                :key="item.id + '-zt'"
                :label="item.value"
              >{{ item.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="close">取消</el-button>
      <el-button :loading="loading" type="primary" @click="save('form')">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import CustomImageUpload from '@/components/ImageUpload/CustomImageUpload.vue'
import { saveObject, updateObject } from '@/api/system/theme/index'
export default {
  name: 'ThemeEntity',
  components: { CustomImageUpload },
  data() {
    return {
      dialogFormVisible: false,
      loading: false,
      status: [{ id: 0, value: false, label: '禁用' },
        { id: 1, value: true, label: '启用' }],
      form: {
        id: null,
        sfqybjt: false,
        bakImage: '',
        fontSize: 12,
        fontColor: '',
        logo: '',
        opacity: 1,
        backGroundColor: '',
        columnGroundColor: '',
        columnTitleGroundColor: '',
        columnTitleFontColor: '',
        isTitleBackImage: false,
        titleImage: '',
        changeBackGroundColor: '',
        changeFontColor: '',
        bk: 1,
        bkys: '',
        bkdx: '',
        tphd: false,
        zt: true
      }
    }
  },
  methods: {
    openEntity(data) {
      if (undefined !== data && undefined !== data.id && data.id !== null) {
        this.form = Object.assign({}, data)
        this.dialogFormVisible = true
      } else {
        this.dialogFormVisible = true
      }
    },
    save(formName) {
      this.loading = true
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.form.id !== null && undefined !== this.form.id) {
            updateObject(this.form).then(res => {
              this.$message.success('更新成功！')
              this.close()
            }).catch(() => {
              this.loading = false
              this.$message.error('更新失败！')
            })
          } else {
            saveObject(this.form).then(res => {
              this.$message.success('保存成功！')
              this.close()
              this.loading = false
            }).catch(() => {
              this.loading = false
              this.$message.error('保存失败！')
            })
          }
        } else {
          this.loading = false
          return false
        }
      })
    },
    close() {
      this.loading = false
      this.fileList = []
      Object.assign(this.$data.form, this.$options.data.call(this).form)
      this.$refs['form'].resetFields()
      this.dialogFormVisible = false
      this.$emit('fetch-data')
    }
  }
}
</script>

<style scoped>

</style>
